package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.Log;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiCustomerEditBo;
import com.piggy.logi.sys.bo.LogiCustomerExtEditBo;
import com.piggy.logi.sys.bo.LogiCustomerQueryBo;
import com.piggy.logi.sys.service.ILogiCustomerService;
import com.piggy.logi.sys.vo.LogiCustomerExtVo;
import com.piggy.logi.sys.vo.LogiCustomerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 我的客户Controller
 *
 * @author piggy
 * @date 2023-08-16
 */
@Api(value = "我的客户控制器", tags = {"我的客户管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/customer")
public class LogiCustomerController extends BaseController {

    private final ILogiCustomerService iLogiCustomerService;

    /**
     * 查询我的客户列表
     */
    @ApiOperation("查询我的客户列表")
    //@SaCheckPermission("sys:customer:list")
    @PostMapping("/list")
    public TableDataInfo<LogiCustomerExtVo> list(@Validated @RequestBody LogiCustomerQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return iLogiCustomerService.queryPageList(bo);
    }

    /**
     * 导出我的客户列表
     */
    @ApiOperation("导出我的客户列表")
    //@SaCheckPermission("sys:customer:export")
    @LogiLog(title = "我的客户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated LogiCustomerQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setDeptId(SecurityUtils.getDeptId());
        List<LogiCustomerExtVo> list = iLogiCustomerService.queryList(bo);
        ExcelUtil<LogiCustomerExtVo> util = new ExcelUtil<LogiCustomerExtVo>(LogiCustomerExtVo.class);
        util.exportExcel(response, list, "我的客户");
    }

    /**
     * 获取我的客户详细信息
     */
    @ApiOperation("获取我的客户详细信息")
    //@SaCheckPermission("sys:customer:query")
    @GetMapping("/{customerId}")
    public R<LogiCustomerExtVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("customerId") Long customerId) {
        return R.ok(iLogiCustomerService.queryById(customerId));
    }

    /**
     * 新增我的客户
     */
    @ApiOperation("新增我的客户")
    //@SaCheckPermission("sys:customer:add")
    @LogiLog(title = "我的客户", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Integer> add(@Validated @RequestBody LogiCustomerExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setMainDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiCustomerService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改我的客户
     */
    @ApiOperation("修改我的客户")
    //@SaCheckPermission("sys:customer:edit")
    @LogiLog(title = "我的客户", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Integer> edit(@Validated @RequestBody LogiCustomerExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setMainDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiCustomerService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除我的客户
     */
    @ApiOperation("删除我的客户")
    //@SaCheckPermission("sys:customer:remove")
    @LogiLog(title = "我的客户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerIds}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] customerIds) {
        return R.ok(iLogiCustomerService.deleteWithValidByIds(Arrays.asList(customerIds), true) ? 1 : 0);
    }
}
